package 极客算法训练营.chapter04;

import java.util.Stack;

public class 有效的括号 {
    public static void main(String[] args) {

    }
    /*switch (chars[i]) {
        case ']':
            if (peek == '[') {
                continue;
            } else stack.push(chars[i]);
        case '}':
            if (peek == '{') continue;
            else stack.push(chars[i]);
        case ')':
            if (peek == '(') continue;
            else stack.push(chars[i]);
        default:return false;
    }*/
    public boolean isValid(String s) {
        if (s == null || s.length() == 0) return false;
        char[] chars = s.toCharArray();
        Stack<Character> stack = new Stack<>();
        stack.push(chars[0]);
        for (int i = 1; i < chars.length; i++) {
            if (stack.empty()) {
                stack.push(chars[i]);
                continue;
            }
            Character peek = stack.peek();
            if ((chars[i] == ']' && peek == '[') || (chars[i] == ')' && peek == '(') || (chars[i] == '}' && peek == '{')) {
                stack.pop();
                continue;
            } else {
                stack.push(chars[i]);
            }
        }
        return stack.empty();
    }

    /**
     *     public boolean empty() {
     *         return size() == 0;
     *     }
     *     public synchronized int size() {
     *         return elementCount;
     *     }
     *
     *     public synchronized boolean isEmpty() {
     *         return elementCount == 0;
     *     }
     */
}
